<template>
	<view class="content">
		<web-view id="wv" @message="message" :src="src"></web-view>
		<bgyx-tab-bar />
	</view>


</template>

<script>
	import config from '../../config.js'
	import {
		mapState
	} from 'vuex';
	import {
		pub,sub,unsub
	} from '../../common/event';
	import moment from 'moment/moment';
import utils from '../../utils/index.js';
	export default {
		data() {
			return {
				name: '',
				id: '',
				uid: '',
				wv: '',
				src:'',
				info:'',
				status:'normal',
				applyt:0
			}
		},
		components: {

		},
		computed: {
			...mapState({
				
			})
		},
		onBackPress(options) {
			console.log(147, options)
			if (options.from && options.from == 'backbutton') {
				return true
			}
			return false
		},
		onUnload() {
			unsub("socket_touser_applyret_agree",this.applylmagree)
			unsub("socket_touser_applyret_refuse",this.applylmrefuse)
			unsub("socket_liveroomlmenter",this.livetoomlmenter)
			unsub("socket_liveroomchange",this.liveroomchange)
			unsub("socket_liveroomremoveuser",this.liveroomremoveuser)
			unsub(`socket_lmuserleave_${this.uid}`,this.lmuserleave)
			unsub(`socket_lmuserchangeinfo_${this.uid}`,this.lmuserchangeinfo)
			unsub(`socket_liveroomstop`,this.liveroomstop)
			this.$store.commit(`common/setStatus`,'normal')
		},
		onLoad({
			id,uid
		}) {
			sub("socket_touser_applyret_agree",this.applylmagree)
			sub("socket_touser_applyret_refuse",this.applylmrefuse)
			sub("socket_liveroomlmenter",this.livetoomlmenter)
			sub("socket_liveroomchange",this.liveroomchange)
			sub("socket_liveroomremoveuser",this.liveroomremoveuser)
			sub(`socket_lmuserleave_${uid}`,this.lmuserleave)
			sub(`socket_lmuserchangeinfo_${uid}`,this.lmuserchangeinfo)
			sub(`socket_liveroomstop`,this.liveroomstop)
			
			this.$store.commit(`common/setStatus`,'videocall')
			this.id = id
			this.uid = uid
			
			const user = this.$store.state.user.user
			this.name = user.name
			const fileurl = this.$store.state.common.systemInfo.fileurl;
			this.src = `/hybrid/html/enterroom.html?uid=${user.id}&uname=${user.name}&luid=${uid}&id=${id}&ip=${config.ip}&srsUrl=${encodeURIComponent(config.srsUrl)}&fileurl=${encodeURIComponent(fileurl)}`
			
			setTimeout(() => {
				if (!this.wv) {
					let currentWebview = this.$scope.$getAppWebview();
					this.wv = currentWebview.children()[0]
				}
			}, 2000)
			
			setTimeout(()=>{
				this.getData()
			},1000)
		},
		onReady() {
			setTimeout(() => {
				let currentWebview = this.$scope.$getAppWebview();
				this.wv = currentWebview.children()[0]
			}, 500)
		},
		methods: {
			getData:async function(){
				const ret = await this.$u.get(`chat/chat/getdata/liveroom/${this.id}`)
				this.info = ret.data
				
				if(ret.data && ret.data.lmusers){
					this.sendMessage({
						type:'subStream',
						data:ret.data
					})
				}else{
					this.sendMessage({
						type:'subStream',
						data:ret.data
					})
				}
			},
			sendMessage(data) {
				const message = JSON.stringify(data)
				if (!this.wv) {
					let currentWebview = this.$scope.$getAppWebview();
					this.wv = currentWebview.children()[0]
				}
				this.wv.evalJS(`vm.onMessage(${message})`);
			},
			applylm:async function(){
				const retps = await utils.checkPermission("请打开摄像头和麦克风权限")
				if (!retps) {
					uni.showToast({
						title: "请打开摄像头和麦克风权限",
						icon: "none"
					})
					return
				}
				if(this.status == 'applying'){
					uni.showToast({
						title:"请勿频繁申请连麦",
						icon:"none"
					})
					return
				}
				const n = moment().valueOf()
				if( (n - this.applyt) < 1000 * 100 ){
					uni.showToast({
						title:"请勿频繁申请连麦",
						icon:"none"
					})
					return
				}
				this.status = 'applying'
				this.applyt = n
				
				this.$u.post(`chat/chat/liveroom/applylm/${this.uid}`)
				
				uni.showToast({
					title:"连麦申请已发送"
				})
				
				this.sendMessage({
					type:"statuschange",
					data:this.status
				})
			},
			closelm:async function(){
				this.status = 'normal'
				
				this.$u.post(`chat/chat/liveroom/closeapplylm/${this.uid}`)
				
				uni.showToast({
					title:"连麦申请已取消"
				})
				
			},
			closelmx:async function(){
				this.status = 'normal'
				
				this.$u.post(`chat/chat/liveroom/closeapplylmx/${this.uid}`)
				
				uni.showToast({
					title:"连麦已结束"
				})
				
			},
			applylmagree:function(){
				uni.showToast({
					title:"连麦申请通过",
				})
				this.status = "lming"
				this.sendMessage({
					type:"applylmagree",
					data:"lming"
				})
			},
			applylmrefuse:function(){
				uni.showToast({
					title:"连麦申请被拒绝",
					icon:"none"
				})
				
				this.sendMessage({
					type:"statuschange",
					data:"normal"
				})
			},
			livetoomlmenter:function(e){
				const user = this.$store.state.user.user
				if(e.uid == this.uid && e.id == this.id && user.id != e.user.id){
					this.sendMessage({
						type:"sublmStream",
						data:e.user
					})
				}
			},
			liveroomchange:function(e){
				if(e.uid != this.uid){
					return
				}
				if(e.uid == this.uid){
					this.sendMessage({
						type:"liveroomchange",
						data:e
					})
				}
			},
			liveroomremoveuser:async function(e){
				if(e.id != this.uid){
					return
				}
				const user = this.$store.state.user.user
				if(e.user.id == user.id){
					try{
						this.sendMessage({
							type:"letclosemx",
							data:{}
						})
					}catch(e){
						//TODO handle the exception
					}
				}else{
					
					this.sendMessage({
						type:"liveroomremoveuser",
						data:e.user
					})
					
				}
			},
			lmuserleave: async function(e){
				this.sendMessage({
					type:"lmuserleave",
					data:e
				})
				
			},
			lmuserchangeinfo: async function(e){
				this.sendMessage({
					type:"lmuserchangeinfo",
					data:e
				})
				
			},
			liveroomstop: async function(e){
				const _this = this
				uni.showModal({
					title:"直播结束",
					content:"直播已结束，下次再来吧~",
					success() {
						_this.sendMessage({
							type:"letexit",
							data:{}
						}) 
					}
				})
			},
			message: function(e) {
				if(e && e.detail && e.detail.data && e.detail.data.length > 0){
					const user = this.$store.state.user.user
					const message = e.detail.data[0]
					console.log(111,message)
					
					
					if(message.type == "statuschange"){
						this.status = message.data
					}
					
					
					
					if(message.type == "pubmystreamdone"){
						this.$u.post(`chat/chat/liveroom/lmpubdone`,{
							id:this.id,
							uid:this.uid,
						})
					}
					
					if(message.type == "applylm"){
						this.applylm()
					}
					
					if(message.type == "closelm"){
						this.closelm()
					}
					
					if(message.type == "closelmx"){
						this.closelmx()
					}
					
					if(message.type == "muteaudio"){
						this.$u.put(`chat/chat/lmuserchangeinfo/${this.uid}`,{
							video:message.data.video,
							audio:message.data.audio
						})
					}
					
					if(message.type == "mutevideo"){
						this.$u.put(`chat/chat/lmuserchangeinfo/${this.uid}`,{
							video:message.data.video,
							audio:message.data.audio
						})
					}
					
					if(message.type == "exit"){
						this.$store.commit(`common/setStatus`,'normal')
						this.$u.post(`chat/chat/leaveliveroom/${this.uid}`)
						uni.navigateBack()
					}
					
					if(message.type == "closedone"){
						
						uni.navigateBack()
					}
					
				}
			},
		}
	}
</script>


<style scoped>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		width: 100%;
		height: 100vh;
		overflow-y: hidden;
		padding-bottom: 120rpx;
		border-start-end-radius: 32rpx;
		border-start-start-radius: 32rpx;
	}
</style>